//
//  MemoryView.m
//  Memory5
//
//  Created by Jin Hu on 2010/08/08.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MemoryView.h"
#import "State.h"
#import "MemoryStage.h"
@implementation MemoryView


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		state=S_PLAY;
		cardFlipped[0]=NULL;
		cardFlipped[1]=NULL;
		correct=0;
		total=24;
	}
	return self;
}
- (id)initWithFrame:(CGRect)frame stage:(MemoryStage*)stage{
    if ((self = [super initWithFrame:frame])) {
		state=S_PLAY;
		cardFlipped[0]=NULL;
		cardFlipped[1]=NULL;
		correct=0;
		total=24;
		stageNr = stage.currentStage;
		[self initStage:stage];
		[self initSound];
	}
	return self;
}
-(void)initStage:(MemoryStage*)stage{
	self.image = [stage getBackground];
	cards = [stage shuffelCards];
	for (int i = 0; i<[cards count]; i++) {
		Card* c = [cards objectAtIndex:i];
		[c addTarget:self action:@selector(flipToFront:) forControlEvents: UIControlEventTouchUpInside];
		[self insertSubview:c atIndex:0];
		
	}
}
- (void)initSound{
	SystemSoundID aSoundID;
	NSBundle *mainBundle = [NSBundle mainBundle];
	//[self initFamily];
	NSURL *aFileURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"farSound" ofType:@"caf"] isDirectory:NO];
	OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
	if (error == kAudioServicesNoError) { // success
		flip = aSoundID;
	}
	aFileURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"levelSound" ofType:@"caf"] isDirectory:NO];
	error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
	if (error == kAudioServicesNoError) { // success
		match = aSoundID;
	}
}
- (id)init{
	cardFlipped[0]=NULL;
	cardFlipped[1]=NULL;
	correct=0;
	total=24;
	SystemSoundID aSoundID;
	NSBundle *mainBundle = [NSBundle mainBundle];
	//[self initFamily];
	NSURL *aFileURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"farSound" ofType:@"caf"] isDirectory:NO];
	OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
	if (error == kAudioServicesNoError) { // success
		flip = aSoundID;
	}
	aFileURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"levelSound" ofType:@"caf"] isDirectory:NO];
	error = AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &aSoundID);
	if (error == kAudioServicesNoError) { // success
		match = aSoundID;
	}
	
	
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
    [super dealloc];
}

-(IBAction)flipToFront:(id)sender{
	if(cardFlipped[0] !=NULL && cardFlipped[1]!=NULL){
		[self flipToBack: cardFlipped[0]];
		[self flipToBack: cardFlipped[1]];
		cardFlipped[0]=NULL;
		cardFlipped[1]=NULL;
	}
	
	Card* current = (Card*)sender;
	
	[current flipToFront:current];
	[current removeTarget: self action: @selector(flipToFront:) forControlEvents: UIControlEventTouchUpInside];
	
	if(cardFlipped[0]==NULL){
		cardFlipped[0]=current;
		AudioServicesPlaySystemSound(flip);
		
	}else{
		if([[cardFlipped[0] getFace] compare:[current getFace]]==0){	
			correct+=2;
			cardFlipped[0]=NULL;
			AudioServicesPlaySystemSound(match);
		}else{
			cardFlipped[1]=current;
			AudioServicesPlaySystemSound(flip);
		}
		
	}
	
	if(correct==total){
		[self flipAllBack:current];
		if(stageNr==7)
			state=S_END;
		else
			state=S_TRANSITION;
	}
	
}


-(IBAction)flipAllBack:(id)sender{
	for(int i=0;i<total;i++){
		Card* c = [cards objectAtIndex:i];
		[c flipToBack:c]; 
	}
}

-(IBAction)flipToBack:(id)sender{
	Card* current = (Card*)sender;
	[current flipToBack:current];
	[current addTarget: self action: @selector(flipToFront:) forControlEvents: UIControlEventTouchUpInside];
}
-(int) stateMachine{
	return state;
}
-(void)drawRect:(CGRect)rect{
	[self.image drawInRect:rect];
	[super drawRect:rect];
}
@end

